import JSEncrypt from 'jsencrypt'
import { Message } from 'element-ui'

// RSA加密 data:要加密的数据  key:加密公钥 返回加密后的字符串
export const rsaEncrypt = (data, key) => {
  const encryptor = new JSEncrypt()
  encryptor.setPublicKey(key)
  const result = encryptor.encrypt(data)
  if (!result) {
    Message.error('密码加密失败')
    throw new Error('RSA加密失败')
  }
  return result
}

// 解析资源状态消息
export const parseResStatusMsg = (row) => {
  let tooltipTitle = row.status_message && row.status === 'Not Running' ? 'Failed' : row.status
  switch (tooltipTitle) {
    case 'Running':
    case 'running(Master)':
    case 'running(Slave)':
      tooltipTitle = '存在节点上的资源处于Running状态且不存在错误记录'
      break
    case 'Not Running':
      tooltipTitle = '存在节点上的资源均为Not Running状态'
      break
    case 'Unmanaged':
      tooltipTitle = '存在节点上的资源处于维护模式'
      break
    case 'Running but failed':
      tooltipTitle = '存在节点上的资源处于Running状态,存在错误记录'
      break
    case 'Failed':
    case 'Stop Failed':
      tooltipTitle = '资源无法在任一节点运行'
      break
    default:
      tooltipTitle = row.status || '未知状态'
  }
  return tooltipTitle
}

// 解析资源状态颜色
export const parseResStatusColor = (row) => {
  switch (row.status) {
    case 'Running':
      return 'success'
    case 'Not Running':
      if (row.status_message) {
        return 'danger'
      }
      return 'info'
    case 'Unmanaged':
      return 'primary'
    case 'Running but failed':
      return 'warning'
    case 'Failed':
    case 'Stop Failed':
      return 'danger'
    case 'running(Master)':
    case 'running(Slave)':
      return 'success'
    default:
      return 'info'
  }
}
